<?php

namespace app\common\model;

use think\Model;
use think\Session;
use think\Db;
use app\common\model\Base;

class SystemRoute extends Base {

    public function getTypeAttr($value) {
        $status = [0 => '', 1 => '自定义'];
        return isset($status[$value]) ? $status[$value] : '';
    }

    public function route_reset_article() {


        $dataList = [];
        // 文章分类
        $category = model('article_cat')->where('alias', '<>', '')->select();
        foreach ($category as $key => $value) {

            $path = $value['path'];
            if ($path) {
                $path_arr = explode('-', $path);

                $alias = [];
                for ($i = 1; $i < count($path_arr); $i++) {
                    $alias[] = db('article_cat')->where('id', $path_arr[$i])->value('alias');
                }

                if ($alias) {
                    $dataList[] = ['full_url' => 'index/index/lists_article?id=' . $value['id'], 'url' => implode('/', $alias) . '$', 'type' => 0];
                    $dataList[] = ['full_url' => 'index/index/views_article', 'url' => implode('/', $alias) . '/:id', 'type' => 0];
                }
            }
        }
        // 文章详情
        $pages = model('article')->where('alias', 'not null')->select();
        foreach ($pages as $value2) {
            if ($value2['alias'] != '') {

                $alias = $value2['alias'];

                //把分类的 别名也加上去
                $cat_alias = model('article_cat')->where('id', $value2['cid'])->value('alias');

                if (!empty($cat_alias)) {
                    $alias = $cat_alias . '/' . $alias;
                }

                $dataList[] = ['full_url' => 'index/index/views_article?id=' . $value2['id'], 'url' => $alias, 'type' => 0];
            }
        }

        return $dataList;
    }

    public function route_reset_goods() {

        $dataList = [];

        // 分类
        $category = model('goods_cat')->select();
        foreach ($category as $key => $value) {
            if ($value['alias'] != '') {
                $dataList[] = ['full_url' => 'index/index/lists_goods?id=' . $value['id'], 'url' => $value['alias'] . '$', 'type' => 0];
                $dataList[] = ['full_url' => 'index/index/views_goods', 'url' => $value['alias'] . '/:id', 'type' => 0];
            }
        }
        // 详情
        $pages = model('goods')->where('alias', 'not null')->select();
        foreach ($pages as $value2) {
            if ($value2['alias'] != '') {

                $alias = $value2['alias'];

                //把分类的 别名也加上去
                $cat_alias = model('goods_cat')->where('id', $value2['cid'])->value('alias');

                if (!empty($cat_alias)) {
                    $alias = $cat_alias . '/' . $alias;
                }

                $dataList[] = ['full_url' => 'index/index/views_goods?id=' . $value2['id'], 'url' => $alias, 'type' => 0];
            }
        }

        return $dataList;
    }

    /**
     * @title 重新生成路由文件
     */
    public function route_reset() {

        model('system_route')->where('type = 0 or type is null')->delete();

        Db::execute('ALTER TABLE tb_system_route AUTO_INCREMENT=0');

        $article_exist = Db::query('show tables like "tb_article"');
        $goods_exist = Db::query('show tables like "tb_goods"');

        $lists_article = count($article_exist) ? $this->route_reset_article() : [];

        $lists_goods = count($goods_exist) ? $this->route_reset_goods() : [];

        $dataList = array_merge($lists_article, $lists_goods);

        if (!empty($dataList)) {
            $count = $this->saveAll($dataList);
        } else {
            $count = 0;
        }

        if ($count = $this->route_file_put()) {
            return true;
        } else {
            return false;
        }
    }

    public function route_file_put() {
        $lists = $this->select();
        // print_r($lists);
        $route_cfg = [];
        foreach ($lists as $key => $value) {
            $route_cfg[$value['url']] = $value['full_url'];
        }
        // $route_file = APP_DIR.DIRECTORY_SEPARATOR.
        $route_file = ROOT_PATH . '/route/custom.php';
        $count2 = file_put_contents($route_file, "<?php\t" . chr(10) . "return " . stripslashes(var_export($route_cfg, true)) . ";");
        return $count2;
    }

}
